package pl.edu.wsinf.lukaszwalczak.emailclient.controller;

import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Task;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.TaskRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.view.TaskView;

@Component
public class TaskController {

    @Inject
    private TaskView view;
    @Inject
    private TaskRepository taskRepository;

    public void showWindow() {
        view.show();
    }

    public void createTask(String description) {
        if (description != null && !description.isEmpty()) {
            Task task = new Task();
            task.setDescription(description);
            taskRepository.save(task);
        }
    }

    public void updateDescription(Long id, String description) {
        Task task = taskRepository.findOne(id);
        if (task != null && description != null) {
            task.setDescription(description);
            taskRepository.save(task);
        }
    }

    public void updateDone(Long id, Boolean done) {
        Task task = taskRepository.findOne(id);
        if (task != null && done != null) {
            task.setDone(done);
            taskRepository.save(task);
        }
    }

    public List<Task> getTasks() {
        return taskRepository.findAll();
    }

    public void removeTask(Task task) {
        taskRepository.delete(task);
    }
}
